/*
 * Copyright (c) 2015. 石头哥哥
 */

package com.comom.manager.controller.testcontroller;

import com.comom.manager.kit.ObjectMapperFactory;
import com.comom.manager.kit.oss.OssConstants;
import com.comom.manager.kit.oss.OssService;
import com.comom.manager.model.TestUser;
import com.comom.manager.model.User;
import com.comom.manager.model.vo.ObjectVo;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by chenlei on 15/7/9.
 * PROJECT_NAME: server-java-mengchong
 * PACKAGE_NAME: com.mengchong.server.controller
 */
@RestController
public class TestController {


    @Resource
    private OssService ossService;

    @Resource
    private ObjectMapperFactory objectMapperFactory;


    @RequestMapping("/test0")
    public void testResponse(HttpServletResponse response) throws IOException {
        PrintWriter printWriter = response.getWriter();
        printWriter.write("success");
        printWriter.flush();  // 推送写入的数据
        printWriter.close();    //Closes the stream and releases any system resources associated
    }


    /**
     * {
     * "code": 0,
     * "msg": "token is error",
     * "data": {
     * "user": {
     * "uid": "502786",
     * "accessToken":"2.00JccF5CPW1STDd8d03d413dYO4NPE",
     * "icon":"http://121.43.228.77:8081/img/me.jpg",
     * "nickname":"Swei大哥"
     * }
     * }
     * }
     */
    @RequestMapping(value = "/test")
    public String test(HttpServletRequest httpServletRequest) throws JsonProcessingException {
        ObjectVo objectVo = ObjectVo.newBuilder();
        User user = new User();
        user.setUid(1);
        user.setToken("2.00JccF5CPW1STDd8d03d413dYO4NPE");
        user.setIcon( OssConstants.server_url);
        user.setNickname("你好");
        objectVo.getData().setUser(user);
        return objectMapperFactory.writeValueAsString(objectVo);

    }


    /**
     * object mapper test
     *
     * @param args
     */
    public static void main(String[] args) {
        final ObjectMapper mapper = new ObjectMapper();
        mapper.getFactory().configure(JsonGenerator.Feature.ESCAPE_NON_ASCII, true);

        final Map<String, Object> objectMap = new HashMap<String, Object>();
        objectMap.put("code", 0);
        objectMap.put("msg", "token is error");
        TestUser user = new TestUser();
        user.setUid("502786");
        user.setAccessToken("2.00JccF5CPW1STDd8d03d413dYO4NPE");
        user.setIcon("/img/图片.jpg");
        user.setNickname("你好");
        objectMap.put("data", user);


        Thread thread1 = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    for (; ; ) {
                        mapper.readTree(mapper.writeValueAsString(objectMap));
                        Thread.sleep(10);
                        System.out.println(Thread.currentThread().getName());
                    }

                } catch (IOException e) {
                    e.printStackTrace();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });


        Thread thread2 = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    for (; ; ) {
                        mapper.readTree(mapper.writeValueAsString(objectMap));
                        Thread.sleep(10);
                        System.out.println(Thread.currentThread().getName());
                    }

                } catch (IOException e) {
                    e.printStackTrace();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });


        Thread thread3 = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    for (; ; ) {
                        mapper.readTree(mapper.writeValueAsString(objectMap));
                        Thread.sleep(10);
                        System.out.println(Thread.currentThread().getName());
                    }

                } catch (IOException e) {
                    e.printStackTrace();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        thread1.start();
        thread2.start();
        thread3.start();


    }


    static String getUnicode(String s) {
        StringBuilder out = new StringBuilder("");
        try {
            byte[] bytes = s.getBytes("unicode");
            for (int i = 0; i < bytes.length - 1; i += 2) {
                out.append("\\u");
                String str = Integer.toHexString(bytes[i + 1] & 0xff);
                for (int j = str.length(); j < 2; j++) {
                    out.append("0");
                }
                String str1 = Integer.toHexString(bytes[i] & 0xff);
                out.append(str1);
                out.append(str);
            }
            return out.toString();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            return null;
        }
    }


    @RequestMapping("/testwriter")
    public void testWriter(HttpServletResponse response) throws IOException {
        PrintWriter writer = response.getWriter();
        writer.write("success0");
        writer.flush();
        writer.close();
    }


}
